import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService } from '@angular-mdl/core';
import { ConfirmationService, SelectItem } from 'primeng/primeng';
import { ProcDef } from '../freeflow.model';
import { FreeflowService } from '../freeflow.service';

@Component({
  selector: 'ff-proc',
  templateUrl: './ffproc.component.html',
  styleUrls: ['./ffproc.component.scss']
})
export class FfProcComponent implements OnInit {
  @Input()
  set procDefId(procDefId: number) {
    if (!procDefId) return;
    this.FreeflowService.showProcDefDetail(procDefId).subscribe(procDef => {
      this.procDef = procDef;
      this.buildForm();
    })
  }
  get procDefId(): number { return this.procDef.procDefId; }

  procForm: FormGroup;
  procDef: ProcDef = new ProcDef({});
  timeStrategyArray: SelectItem[] = [ { 'label': '正常计时', 'value': 1 }];
  timeUnitArray: SelectItem[] = [ { 'label': '小时', 'value': 1 }, { 'label': '天', 'value': 2 }, { 'label': '周', 'value': 3 }];
  constructor(
    private fb: FormBuilder,
    private FreeflowService: FreeflowService,
    private mdlSnackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService) {
  }

  ngOnInit() {
    this.buildForm();
  }

  /**保存流程基本信息 */
  saveUpdate() {
    this.FreeflowService.saveUpdateProc(this.procForm.value)
      .subscribe(data => {
        this.FreeflowService.announceMission('proc_' + data);
        this.mdlSnackbarService.showToast('数据保存成功！');
      });
  }

  /**删除 */
  delete() {
    if (this.procDef.procDefId) {
      this.confirmationService.confirm({
        header: '确认',
        message: '您确定要删除选择的记录吗？',
        accept: () => {
          this.FreeflowService.deleteProc(this.procDef.procDefId).subscribe(data => {
            this.FreeflowService.announceMission('');
            this.mdlSnackbarService.showToast("数据删除成功！");
          });
        }
      });
    }
  }
  buildForm(): void {
    this.procForm = this.fb.group({
      'procDefId': [this.procDef.procDefId],
      'procDefName': [this.procDef.procDefName],
      'procDesc': [this.procDef.procDesc, [
        Validators.required,
        Validators.maxLength(100)
      ],
      ],
      'timeStrategy': [this.procDef.timeStrategy],
      'timeUnitId': [this.procDef.timeUnitId],
      'timeUnitName': [this.procDef.timeUnitName],
      'timeLimit': [this.procDef.timeLimit],
      'timeWarn': [this.procDef.timeWarn],
      'bizId': [this.procDef.bizId],
      'bizName': [this.procDef.bizName],
      'createTime': [this.procDef.createTime],
      'createUser': [this.procDef.createUser]
    });

    this.procForm.valueChanges
      .subscribe(data => this.onValueChanged(data));

    this.onValueChanged();
  }
  onValueChanged(data?: any) {
    if (!this.procForm) { return; }
    const form = this.procForm;

    for (const field in this.formErrors) {
      this.formErrors[field] = '';
      const control = form.get(field);

      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }
  }

  formErrors = {
    'procDefId': '',
    'procDefVer': '',
    'procDefName': '',
    'displayOrder': '',
    'procDefDesc': ''
  };


  validationMessages = {
    'procDefId': {
      'required': 'Name is required.'
    },
    'procDefVer': {
      'required': 'Code is required.'
    },
    'procDefName': {
      'required': 'Power is required.'
    },
    'displayOrder': {
      'required': 'Power is required.'
    },
    'procDefDesc': {
      'required': 'Power is required.'
    }
  };
}
